//
//  ClubMyGiftView.swift
//  77Club
//
//  Created by Apple on 2024/9/23.
//

import UIKit


class ClubMyGiftListCell: UITableViewCell, UITableViewDelegate, UITableViewDataSource {
    
    private let bgImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("Mine_gift_bg").end()
    private let iconImageV = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("Mine_gift_logo").end()
    private let titleLabel = c_make_ex(UILabel.self)
        .color("#7B7D80").font(14.font_semiBold).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color("#7B7D80").font(12.font_regular).end()
    private let timeLabel = c_make_ex(UILabel.self)
        .color("#7B7D80").font(12.font_regular).alignment(.right).end()
    private let stateLabel = c_make_ex(UILabel.self)
        .corner(12.0.Scale).text("已领取").color(.white)
        .bgColor("#AEB0B3").alignment(.center).end()
    private let directionImg = c_make_ex(UIButton.self)
        .image("Mine_gift_down").image("Mine_gift_up",state: .selected).end()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .white
        tableView.showsVerticalScrollIndicator = false
        tableView.register(ClubMyGiftContentCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        tableView.rowHeight = 20.0.Scale
        return tableView
    }()
    private var titles = [String]()

    var model:ClubMineGiftsModel?{
        didSet{
            guard let md = model else { return }
            
            titleLabel.text = md.cardname
            nameLabel.text = md.gname
            timeLabel.text = md.end_time + " 到期"
            let one = "1.礼包名称：" + md.cardname
            let two = "2.适用游戏：" + md.gname
            let three = "3.开始时间：" + md.start_time
            let four = "4.结束时间：" + md.end_time
            titles = [one,two,three,four]
            self.tableView.reloadData()
            directionImg.isSelected = md.isShow
            if md.isShow {
                tableView.heightConstraint?.constant = 80.0.Scale
            }else{
                tableView.heightConstraint?.constant = 0
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            bgImageView.subviews{
                iconImageV
                titleLabel
                nameLabel
                timeLabel
                stateLabel
                directionImg
            }
            tableView
        }
        
        bgImageView.leading(12.0.Scale).trailing(12.0.Scale).top(12.0.Scale).height(90.0.Scale)
        iconImageV.leading(12.0.Scale).size(70.0.Scale).centerVertically()
        titleLabel.leading(112.0.Scale).top(18.0.Scale).trailing(120.0.Scale)
        nameLabel.leading(112.0.Scale).top(40.0.Scale).trailing(120.0.Scale)
        timeLabel.leading(112.0.Scale).top(60.0.Scale).trailing(120.0.Scale)
        stateLabel.trailing(12.0.Scale).width(72.0.Scale).height(24.0.Scale).centerVertically()
        directionImg.trailing(12.0.Scale).bottom(6.0.Scale).size(14.0.Scale)
        tableView.leading(12.0.Scale).trailing(12.0.Scale).top(92.0.Scale).height(0).bottom(10.0.Scale)
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return titles.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "ClubMyGiftContentCell", for: indexPath) as! ClubMyGiftContentCell
        cell.title = titles[indexPath.row]
        return cell
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


class ClubMyGiftContentCell: UITableViewCell {
    
    private let titleLabel = c_make_ex(UILabel.self)
        .color("#7B7D80").font(12.font_semiBold).end()

    var title:String = ""{
        didSet{
            
            titleLabel.text = title
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            titleLabel
        }
        
        titleLabel.leading(12.0.Scale).trailing(12.0.Scale).centerVertically()
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
